package gao.xiaolei.dao;

import java.time.LocalDateTime;
import java.util.List;

import gao.xiaolei.entity.TestRequirementCheck;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository("TestRequirementCheckDao")
public interface TestRequirementCheckDao extends JpaRepository<TestRequirementCheck, String>, CheckMethod<TestRequirementCheck> {

    //根据父节点Id查找
    public List<TestRequirementCheck> findByParentId(String parentId);

    //根据审核人和项目Id查找已经完成的审核记录,(只返回id,测试需求Id,是否通过审核,审核日期,leaf,parentId),并根据时间由最新的开始排序
    @Query("select id,targetId,isPass,checkDate,leaf,parentId from TestRequirementCheck where checkMan=:checkMan and isFinish=:isFinish and parentId=:parentId and projectId=:projectId order by checkDate DESC")
    public List<Object[]> findByCheckManAndIsFinishAndParentIdAndProjectId(@Param("checkMan") String checkMan,
                                                                           @Param("isFinish") Integer isFinish,
                                                                           @Param("parentId") String parentId,
                                                                           @Param("projectId") String projectId);

    //获取父节点
    @Query("select parentId from TestRequirementCheck where id=:id")
    public String getParentId(@Param("id") String id);

    //获取leaf
    @Query("select leaf from TestRequirementCheck where id=:id")
    public Integer getLeaf(@Param("id") String id);

    //获取children
    @Query("select id from TestRequirementCheck where parentId=:parentId")
    public List<String> getChildren(@Param("parentId") String parentId);

    //获取比某个测试需求的审核树审核时间更新的审核树的数量
    @Query("select count(*) from TestRequirementCheck where targetId=:targetId and checkDate>:checkDate and parentId='root'")
    public Integer getCountByTargetIdAndCheckDateAfter(@Param("targetId") String targetId, @Param("checkDate") LocalDateTime checkDate);
}
